﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HoSoDataConverter
{
    class Tmp
    {
        public string Value { get; set; }
    }

    public class Class1
    {
        public static void Main(string[] args)
        {
            LTQNDataContext db = new LTQNDataContext();
            var dsHoSoCu = from hscu in db.Hsrecords
                           select hscu;
            foreach (var hscu in dsHoSoCu)
            {
                HoSo hsmoi = new HoSo();
                hsmoi.Hsbooks_Id = hscu.Hsbooks_Id;
                db.HoSos.InsertOnSubmit(hsmoi);
                db.SubmitChanges();

                var khungNhap = from khung in db.Hsfieldsattributes
                                where khung.Hsbooks_Id == hsmoi.Hsbooks_Id
                                select khung;
                foreach (var ttin in khungNhap)
                {
                    HoSoDetail chiTiet = new HoSoDetail();
                    chiTiet.HSID = hsmoi.HSID;
                    chiTiet.Hsfields_Id = ttin.Hsfields_Id;

                    var fieldName = ttin.Hsfield.Fieldname;
                    string query = "SELECT "+fieldName+" FROM Hsrecords"
                        + " WHERE Hsrecords_Id = "+hscu.Hsrecords_Id;

                    var results = db.ExecuteQuery<string>(query).First();
                    
                    chiTiet.AttributeValue = results;
                    db.HoSoDetails.InsertOnSubmit(chiTiet);
                    db.SubmitChanges();
                }
                //break;
            }
        }
    }
}
